<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input class="demo"></body>  
    <button class="mybutton">节流</button>
    <script>
        // Object.create()
        // 创建一个新对象 使用现有对象作为新对象的原型 pertotype

        // const demo ={
        //     demo:'demo',
        //     sayHi:function(){
        //         console.log('say hi');
        //     }
        // }
        // console.log(demo,demo instanceof Object)
        // const me = Object.create(demo);
        // console.log(me.__proto__,me.__proto__ === demo);


        // Object.prototype.myCreate = function(obj){
        //     function fu() {}
        //     fu.__proto__ = obj;
        //     return fu
        // }
        // function myCreate(obj){
        //     function F(){};
        //     F.prototype = obj;
        //     return new F();
        // }

        // const me2 = myCreate(demo);
        // // console.log(me2.__proto__ === demo) 
        // console.log(me2 instanceof Function)

        // Object.create(null)

        // instanceof
        // 构造函数的prototype属性是否出现在对象的原型链的任意位置
        // 左边是对象 右边是构造函数
        // function myInstanceof(left,right){
        //     const right_proto = right.prototype;
        //     // let left_proto = left.__proto__
        //     let left_proto = Object.getPrototypeOf(left);

        //     while(true){
        //         if(!left_proto) return false;
        //         if(left_proto === right_proto) return true;

        //         left_proto = Object.getPrototypeOf(left_proto);
        //     }
        // }
        // console.log([] instanceof Array,myInstanceof([],Array))

        // 手写new

        // 手写Promise 相关的

        // 手写 防抖与节流
        function fangdou(fn,time){
            let timer = null;
             return function(){
                 clearTimeout(timer);
                 let args = arguments
                 timer = setTimeout(()=>{
                     fn.apply(this,args);
                 },time)
             }
        }

        const demo = document.querySelector('input');
        demo.addEventListener('input',fangdou(()=>{
            console.log('sadasda')
            console.log(this)
        },2000))


        function throttle(fn,time){
            let curTime = Date.now();
            return function(){
                const context = this,args = arguments,nowTime = Date.now();
                if(nowTime-curTime>time){
                    curTime = nowTime;
                    fn.apply(context,args);
                }
            }
        }

        const btn = document.querySelector('button');

        function clickCallback(){
            console.log('dadasd')
        }
        btn.addEventListener('click',throttle(clickCallback,2000))

       
    </script>
</body>
</html>